package com.healthcare;

import java.io.IOException;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import webservices.HealthCareServerProxy;

/**
 * Servlet implementation class Comment
 */
public class Comment extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Comment() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		/*
		 * public String[] getDocUserPost( String docid){ String response =
		 * null; String[] userPostComment = null; String username =
		 * request.getAttribute("userid").toString(); HealthCareServerProxy
		 * proxy = new HealthCareServerProxy(); try { response =
		 * proxy.getDocUserPosts( username, docid); if(!response.isEmpty()){
		 * userPostComment = response.split("@");
		 * System.out.println(userPostComment[0]);
		 * 
		 * } } catch (RemoteException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } return userPostComment;
		 */

		// }

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String user = request.getSession().getAttribute("user").toString();
		String data = request.getParameter("data").trim();
		String id = data.substring(0, 1);
		String comment = data.substring(3).trim();

		String[] columns = data.split("\\$");
		assert columns.length == 2;
		String comment1 = columns[0].trim();
		String id1 = columns[1];

		DateFormat dateFormat = new SimpleDateFormat(
				"EEE, d MMM yyyy HH:mm:ss Z");
		Date date = new Date();
		HealthCareServerProxy proxy = new HealthCareServerProxy();
		proxy.saveComment(id1, comment1, user, dateFormat.format(date));
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/ListPosts.jsp");
		dispatcher.forward(request, response);

	}

}
